<?php

namespace App\Controller\User;

use App\Base\BaseController4Restful;
use App\Base\Format;
use App\Entity\User;
use App\Repository\ConfigRepository;
use App\Repository\UserRepository;
use App\Service\MoneyService;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;

/**
 * @route("/app")
 */
class AppUserController extends BaseController4Restful
{

    /**
     * @Route("/send_sms",name="AppSendSms")
     */
    public function AppSendSms()
    {
        if (!$phone = $this->getParamRequired("phone")) {return $this->json_out(0,"请输入手机号");}
        $is = new Format();
        if (!$is->isPhone($phone)) {
            return $this->json_out(0,"手机号错误");
        }

        $lastTime = $this->getUserCache("smsLastTime");
        if ($lastTime > time() - 18) {
            return $this->json_out(0,"操作太频繁");
        }
        $smsRecentCount = $this->getUserCache("smsRecentCount");
        $smsRecentDate = $this->getUserCache("smsRecentDate");
        if (!$smsRecentCount || $smsRecentDate != date("Ymd") ) {
            $smsRecentCount = 1;
        }else{
            $smsRecentCount++;
        }
        if ($smsRecentCount > 50) {
            return $this->json_out(0,"尝试次数超出上限");
        }

        require_once __DIR__."/../../../BaseExt/Sms.php";
        $sms = new \Sms();
        $rand = rand(1000,9999);
//        $sms->sendSms($phone,"SMS_174166578",array("code"=>$rand));
        $this->setUserCache("smsCode",$rand);
        $this->setUserCache("smsLastTime",time());
        $this->setUserCache("smsLastPhone",$phone);
        $this->setUserCache("smsRecentCount",$smsRecentCount);
        $this->setUserCache("smsRecentDate",date("Ymd"));
        return $this->json_out(1,"已发送 $rand");
    }


    /**
     * @Route("/check_sms",name="AppUserSMSCheck")
     */
    public function AppUserSMSCheck(Request $request , UserRepository $userRepository)
    {
        if (!$phone = $this->getParamRequired("phone")) {return $this->json_out(0,"请输入手机号");}
        $is = new Format();
        if (!$is->isPhone($phone)) {return $this->json_out(0,"手机号错误");}
        $smsLastPhone = $this->getUserCache("smsLastPhone");
        if ($smsLastPhone != $phone) {return $this->json_out(0,"手机号不一致");}
        $smsCode = $this->getUserCache("smsCode");
        if (!$code = $this->getParamRequired("code")) {return $this->json_out(0,"请输入验证码");}
        if ($code != $smsCode) {return $this->json_out(0,"验证码错误");}
        return $this->json_out(1,"验证码正确");
    }


    /**
     * @Route("/login_sms",name="AppUserSMSLogin")
     */
    public function AppUserSMSLogin(Request $request , UserRepository $userRepository)
    {
        if (!$phone = $this->getParamRequired("phone")) {return $this->json_out(0,"请输入手机号");}
        $is = new Format();
        if (!$is->isPhone($phone)) {return $this->json_out(0,"手机号错误");}
        $smsLastPhone = $this->getUserCache("smsLastPhone");
        if ($smsLastPhone != $phone) {return $this->json_out(0,"手机号不一致");}
        $smsCode = $this->getUserCache("smsCode");
        if (!$code = $this->getParamRequired("code")) {return $this->json_out(0,"请输入验证码");}
        if ($code != $smsCode) {return $this->json_out(0,"验证码错误");}
        $user = $userRepository->findOneBy(array("phone"=>$phone));
        if (!$user) {
            $user = new User();
            $user->setApiToken($phone)
                ->setPhone($phone)->setPic(DEFAULT_USER_HEAD)
                ->setPassword(md5($phone))
                ->setCreateAt(new \DateTime())
                ->setName("用户" . rand(1000,9999));
        }
        $this->myEm()->persist($user);
        $this->myEm()->flush();
        //设置缓存
        $sessionData = $userRepository->createSimpleData($user);
        //启用Cache
        $this->setUserCache('user',$sessionData);
        return $this->json_out(1,"登录成功",$sessionData);
    }

    /**
     * @Route("/reg_sms_password",name="AppUserSMSPasswordReg")
     */
    public function AppUserSMSPasswordReg(Request $request , UserRepository $userRepository , MoneyService $moneyService ,ConfigRepository $configRepository)
    {
        if (!$phone = $this->getParamRequired("phone")) {return $this->json_out(0,"请输入手机号");}
        if (!$password = $this->getParamRequired("password")){return $this->json_out(0,"请输入密码");}
        if (!$code = $this->getParamRequired("code")) {return $this->json_out(0,"请输入验证码");}
        $is = new Format();
        if (!$is->isPhone($phone)) {return $this->json_out(0,"手机号错误");}
        $smsLastPhone = $this->getUserCache("smsLastPhone");
        if ($smsLastPhone != $phone) {return $this->json_out(0,"手机号不一致");}
        $smsCode = $this->getUserCache("smsCode");
        if ($code != $smsCode) {return $this->json_out(0,"验证码错误");}
        $user = $userRepository->findOneBy(array("phone"=>$phone));
        if ($user) {
            return $this->json_out(0,"手机号已占用");
        }else{
            $user = $userRepository->createNewUser($phone,$password);
        }
        $this->myEm()->persist($user);
        $this->myEm()->flush();
        //设置缓存
        $sessionData = $userRepository->createSimpleData($user);
        //启用Cache
        $this->setUserCache('user',$sessionData);
        //注册奖励
        $this->dealWithUserReg($user);
        return $this->json_out(1,"注册成功",$sessionData);
    }




    /**
     * @Route("/login_sms_password",name="AppUserSMSAndPasswordLogin")
     */
    public function AppUserSMSAndPasswordLogin(Request $request , UserRepository $userRepository)
    {
        if (!$phone = $this->getParamRequired("phone")) {return $this->json_out(0,"请输入手机号");}
        if (!$code = $this->getParamRequired("code")) {return $this->json_out(0,"请输入验证码");}
        if(!$password = $this->getParamRequired("password")){return $this->json_out(0,"请输入密码");}
        $is = new Format();
        if (!$is->isPhone($phone)) {return $this->json_out(0,"手机号错误");}
        $smsLastPhone = $this->getUserCache("smsLastPhone");
        if ($smsLastPhone != $phone) {return $this->json_out(0,"手机号不一致");}
        $smsCode = $this->getUserCache("smsCode");
        if ($code != $smsCode) {return $this->json_out(0,"验证码错误");}
        $user = $userRepository->findOneBy(array("phone"=>$phone));
        if ($user->getPassword() != md5($password)) {
            return $this->json_out(0,"手机号或密码错误");
        }

        //每次登陆应该更新一下apiToken,以确保登陆设备的唯一性
        $user = $this->dealWithApiToken($user);

        $this->myEm()->persist($user);
        $this->myEm()->flush();
        //设置缓存
        $sessionData = $userRepository->createSimpleData($user);
        //启用Cache
        $this->setUserCache('user',$sessionData);

        return $this->json_out(1,"登录成功",$sessionData);
    }

    /**
     * @Route("/login_openid",name="AppUserOpenIdLogin")
     */
    public function AppUserOpenIdLogin(Request $request , UserRepository $userRepository)
    {
        if (!$openid = $this->getParamRequired("openid")) {return $this->json_out(0,"微信登陆参数错误");}
        $user = $userRepository->getByOpenid($openid);
        if (!$user) {
            return $this->json_out(0,"未注册的用户");
        }
        //每次登陆应该更新一下apiToken,以确保登陆设备的唯一性
        $user = $this->dealWithApiToken($user);

        $this->myEm()->persist($user);
        $this->myEm()->flush();
        //设置缓存
        $sessionData = $userRepository->createSimpleData($user);
        //启用Cache
        $this->setUserCache('user',$sessionData);

        return $this->json_out(1,"登录成功",$sessionData);
    }


    /**
     * @Route("/reg_sms_openid",name="AppUserSMSOpenIdReg")
     */
    public function AppUserSMSOpenIdReg(Request $request , UserRepository $userRepository , MoneyService $moneyService ,ConfigRepository $configRepository)
    {
        if (!$phone = $this->getParamRequired("phone")) {return $this->json_out(0,"请输入手机号");}
        if (!$openid = $this->getParamRequired("openid")) {return $this->json_out(0,"微信登陆参数错误");}
        if (!$code = $this->getParamRequired("code")) {return $this->json_out(0,"请输入验证码");}
        $is = new Format();
        if (!$is->isPhone($phone)) {return $this->json_out(0,"手机号错误");}
        $smsLastPhone = $this->getUserCache("smsLastPhone");
        if ($smsLastPhone != $phone) {return $this->json_out(0,"手机号不一致");}
        $smsCode = $this->getUserCache("smsCode");
        if ($code != $smsCode) {return $this->json_out(0,"验证码错误");}
        $user = $userRepository->findOneBy(array("phone"=>$phone));
        if ($user) {
            return $this->json_out(0,"手机号已占用");
        }else{
            $user = $userRepository->createNewUser($phone,$phone,$openid);
        }
        $this->myEm()->persist($user);
        $this->myEm()->flush();
        //设置缓存
        $sessionData = $userRepository->createSimpleData($user);
        //启用Cache
        $this->setUserCache('user',$sessionData);
        //注册奖励
        $this->dealWithUserReg($user);

        return $this->json_out(1,"注册成功",$sessionData);
    }


    /**
     * @Route("/reg_simple",name="AppUserRegSimple")
     */
    public function AppUserRegSimple(Request $request , UserRepository $userRepository , MoneyService $moneyService ,ConfigRepository $configRepository)
    {
        if (!$phone = $this->getParamRequired("phone")) {return $this->json_out(0,"请输入手机号");}
        if (!$password = $this->getParamRequired("password")){return $this->json_out(0,"请输入密码");}
        $is = new Format();
        if (!$is->isPhone($phone)) {return $this->json_out(0,"手机号错误");}
        $user = $userRepository->findOneBy(array("phone"=>$phone));
        if ($user) {
            return $this->json_out(0,"手机号已占用");
        }else{
            $user = $userRepository->createNewUser($phone,$password);
        }
        $this->myEm()->persist($user);
        $this->myEm()->flush();
        //设置缓存
        $sessionData = $userRepository->createSimpleData($user);
        //启用Cache
        $this->setUserCache('user',$sessionData);

        if(!$parent = $this->getParamRequired("parent")){$parent = "";}
        if ($parent){
            $parentUser = $userRepository->find($parent);
            if (!$parentUser) {
                $parentUser = $userRepository->getByPhone($parent);
            }
            if ($parentUser) {
                $user->setParent($parentUser);
                $this->myEm()->persist($user);
                $this->myEm()->flush();
            }
        }
        //注册奖励
        $this->dealWithUserReg($user);
        return $this->json_out(1,"注册成功",$sessionData);
    }




    /**
     * @Route("/login_simple",name="AppUserLoginSimple")
     */
    public function AppUserLoginSimple(Request $request , UserRepository $userRepository , MoneyService $moneyService ,ConfigRepository $configRepository)
    {
        if (!$phone = $this->getParamRequired("phone")) {return $this->json_out(0,"请输入手机号");}
        if (!$password = $this->getParamRequired("password")){return $this->json_out(0,"请输入密码");}
        $user = $userRepository->findOneBy(array("phone"=>$phone,"password"=>md5($password)));
        if (!$user) {return $this->json_out(0,"账号或密码错误");}
        //设置缓存
        $sessionData = $userRepository->createSimpleData($user);
        //启用Cache
        $this->setUserCache('user',$sessionData);
        return $this->json_out(1,"登录成功",$sessionData);

    }
    /**
     * @Route("/reset_password",name="AppUserResetPassword")
     */
    public function AppUserResetPassword(Request $request , UserRepository $userRepository , MoneyService $moneyService ,ConfigRepository $configRepository)
    {
        if (!$phone = $this->getParamRequired("phone")) {return $this->json_out(0,"请输入手机号");}
        if (!$code = $this->getParamRequired("code")) {return $this->json_out(0,"请输入验证码");}
        if(!$password = $this->getParamRequired("password")){return $this->json_out(0,"请输入密码");}
        $is = new Format();
        if (!$is->isPhone($phone)) {return $this->json_out(0,"手机号错误");}
        $smsLastPhone = $this->getUserCache("smsLastPhone");
        if ($smsLastPhone != $phone) {return $this->json_out(0,"手机号不一致");}
        $smsCode = $this->getUserCache("smsCode");
        if ($code != $smsCode) {return $this->json_out(0,"验证码错误");}
        $user = $userRepository->findOneBy(array("phone"=>$phone));
        $user->setPassword(md5($password));
        //每次登陆应该更新一下apiToken,以确保登陆设备的唯一性
        $user = $this->dealWithApiToken($user);

        $this->myEm()->persist($user);
        $this->myEm()->flush();
        //设置缓存
        $sessionData = $userRepository->createSimpleData($user);
        //启用Cache
        $this->setUserCache('user',$sessionData);
        return $this->json_out(1,"修改成功",$sessionData);
    }
    /**
     * @Route("/reset_password_pay",name="AppUserResetPasswordPay")
     */
    public function AppUserResetPasswordPay(Request $request , UserRepository $userRepository , MoneyService $moneyService ,ConfigRepository $configRepository)
    {
        if (!$phone = $this->getParamRequired("phone")) {return $this->json_out(0,"请输入手机号");}
        if (!$code = $this->getParamRequired("code")) {return $this->json_out(0,"请输入验证码");}
        if(!$password = $this->getParamRequired("password")){return $this->json_out(0,"请输入密码");}
        $is = new Format();
        if (!$is->isPhone($phone)) {return $this->json_out(0,"手机号错误");}
        $smsLastPhone = $this->getUserCache("smsLastPhone");
        if ($smsLastPhone != $phone) {return $this->json_out(0,"手机号不一致");}
        $smsCode = $this->getUserCache("smsCode");
        if ($code != $smsCode) {return $this->json_out(0,"验证码错误");}
        $user = $userRepository->findOneBy(array("phone"=>$phone));
        $user->setPasswordPay(md5($password));
        //每次登陆应该更新一下apiToken,以确保登陆设备的唯一性
        $user = $this->dealWithApiToken($user);
        $this->myEm()->persist($user);
        $this->myEm()->flush();
        //设置缓存
        $sessionData = $userRepository->createSimpleData($user);
        //启用Cache
        $this->setUserCache('user',$sessionData);
        return $this->json_out(1,"修改成功",$sessionData);
    }

    /**
     * @Route("/reset_phone",name="AppUserResetPhone")
     */
    public function AppUserResetPhone(Request $request , UserRepository $userRepository , MoneyService $moneyService ,ConfigRepository $configRepository)
    {
        if (!$phone = $this->getParamRequired("phone")) {return $this->json_out(0,"请输入手机号");}
        if (!$code = $this->getParamRequired("code")) {return $this->json_out(0,"请输入验证码");}
        $is = new Format();
        if (!$is->isPhone($phone)) {return $this->json_out(0,"手机号错误");}
        $smsLastPhone = $this->getUserCache("smsLastPhone");
        if ($smsLastPhone != $phone) {return $this->json_out(0,"手机号不一致");}
        $smsCode = $this->getUserCache("smsCode");
        if ($code != $smsCode) {return $this->json_out(0,"验证码错误");}
        $user = $this->needToBeUser();
        $user->setPhone($phone);
        //每次登陆应该更新一下apiToken,以确保登陆设备的唯一性
        $user = $this->dealWithApiToken($user);
        $this->myEm()->persist($user);
        $this->myEm()->flush();
        //设置缓存
        $sessionData = $userRepository->createSimpleData($user);
        //启用Cache
        $this->setUserCache('user',$sessionData);
        return $this->json_out(1,"修改成功",$sessionData);
    }


    private function dealWithApiToken(User $user)
    {
        return $user;
    }

    private function dealWithUserReg(User $user)
    {
        return $user;
    }

}
